<?php

/**
 * Author:sky_php@qq.com
 */
set_time_limit(0);
include "common.php";
$staticurl = "http://wenba.xiangrikui.com/HuaTi/";
$categorylist = array(
    'RenShouBaoXian' => 2,
    'ShaoErBaoXian' => 3,
    'SheBao' => 4
);
$pagesize = 3;
foreach ($categorylist as $cname => $cid) {
    for ($i = 1; $i <= $pagesize; $i++) {
        $html = file_get_html($staticurl . $cname . '/' . $i . '.html');
        foreach ($html->find(".hufutext p a") as $link) {
            parsetodb($link->href, $cid);
        }
        unset($html);
    }
}

/* 采集到数据库中 */

//parsetodb("http://wenba.xiangrikui.com/250114.html", 2);

function parsetodb($url, $cid) {
    global $db, $tablepre;
    $html = file_get_html($url);
    $qtitle = str_replace("&nbsp;", "", iconv("GBK", "UTF-8", $html->find("h2", 0)->plaintext));
    $qdesc = iconv("GBK", "UTF-8", $html->find("h2", 0)->next_sibling()->innertext);
    $qauthorstr = iconv("GBK", "UTF-8", $html->find(".pa4 span", 0)->find("a", 0)->plaintext);
    $qauthor = substr($qauthorstr, 0, strpos($qauthorstr, "("));
    //导入用户
    $quid = $db->result_first("SELECT uid FROM " . $tablepre . "user WHERE username='$qauthor' LIMIT 0,1");
    if (!$quid) {
        $db->query("INSERT INTO " . $tablepre . "user(username,password,email,regip,regtime,`lastlogin`) values ('$qauthor','" . md5('111111') . "','','127.0.0.1'," . time() . "," . time() . ")");
        $quid = $db->insert_id();
    }
    //导入问题
    $qtime = time() + rand(100, 1000000);
    $endtime = $qtime + 30 * 86400;
    $db->query("REPLACE INTO " . $tablepre . "question SET cid='$cid',cid1='$cid',authorid='$quid',author='$qauthor',title='$qtitle',description='$qdesc',price='0',time='$qtime',endtime='$endtime',status='1',ip='127.0.0.1'");
    $qid = $db->insert_id();
    $db->query("UPDATE " . $tablepre . "category SET questions=questions+1 WHERE  id IN ($cid) ");
    $db->query("UPDATE " . $tablepre . "user SET questions=questions+1 WHERE  uid =$quid");
    //导入回答
    foreach ($html->find(".pa2") as $ret) {
        $aauthor = iconv("GBK", "UTF-8", $ret->find(".hd", 0)->find("a", 0)->plaintext);
        $acontent = iconv("GBK", "UTF-8", $ret->find(".b_wangjing", 0)->innertext);
        $auid = $db->result_first("SELECT uid FROM " . $tablepre . "user WHERE username='$aauthor' LIMIT 0,1");
        if (!$auid) {
            $db->query("INSERT INTO " . $tablepre . "user(username,password,email,regip,regtime,`lastlogin`) values ('$aauthor','" . md5('111111') . "','','127.0.0.1'," . time() . "," . time() . ")");
            $auid = $db->insert_id();
        }
        $atime = time() + rand(100, 1000000);
        $db->query("INSERT INTO " . $tablepre . "answer SET qid='$qid',title='$qtitle',author='$aauthor',authorid='$auid',time='$atime',content='$acontent',status=1,ip='127.0.0.1'");
        $db->query("UPDATE " . $tablepre . "question SET  answers=answers+1  WHERE id=" . $qid);
        $db->query("UPDATE " . $tablepre . "user SET answers=answers+1 WHERE  uid =$auid");
        unset($aauthor, $acontent, $atime, $auid);
    }
    unset($qauthor, $qdesc, $qauthorstr, $qid, $quid, $qtime, $endtime, $html);
}
?>